package com.yiyw.herostory.handler;

import com.yiyw.herostory.manage.ChannelGroupManage;
import com.yiyw.herostory.pojo.UserEntity;
import com.yiyw.herostory.proto.GameMsgProtocol;
import com.yiyw.herostory.service.UserService;
import com.yiyw.herostory.util.SpringUtil;
import io.netty.channel.ChannelHandlerContext;

/**
 * @Author yiyw
 * @Description TODO
 * @Date 2022/8/21 15:45
 **/
public class SelectHeroCmdHandler implements ICmdHandler<GameMsgProtocol.SelectHeroCmd> {

    UserService userService = SpringUtil.getBean(UserService.class);
    @Override
    public void handler(ChannelHandlerContext ctx, GameMsgProtocol.SelectHeroCmd msg) {
        // 获取用户 ID
        int id = ChannelGroupManage.getUserIdByChannel(ctx.channel());
        if (id == -1) {
            return;
        }
        String heroAvatar = msg.getHeroAvatar();
        UserEntity entity = new UserEntity();
        entity.setHeroAvatar(heroAvatar);
        entity.setUserId(id);
        userService.updateUserHeroAvatar(entity);

        GameMsgProtocol.SelectHeroResult.Builder result = GameMsgProtocol.SelectHeroResult.newBuilder();
        result.setHeroAvatar(heroAvatar);

        ctx.channel().writeAndFlush(result.build());

    }
}
